#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <map>

using namespace std;

struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;

    TreeNode() : val(0), left(nullptr), right(nullptr)
    {
    }

    TreeNode(int x) : val(x), left(nullptr), right(nullptr)
    {
    }

    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right)
    {
    }
};

class Solution 
{
public:
    TreeNode* trimBST(TreeNode* root, int low, int high) 
    {
        if(root == nullptr)
        {
            return nullptr;
        }
        if(root->val >= low && root->val <= high)
        {
            root->left = trimBST(root->left, low, high);
            root->right = trimBST(root->right, low, high);
            return root;
        }
        else if (root->val < low)
        {
            return trimBST(root->right, low, high);
        }
        else if (root->val > high)
        {
            return trimBST(root->left, low, high);
        }
        return root;
    }
};

// 辅助函数：从数组构建二叉搜索树（层序遍历方式，-1表示null）
TreeNode *buildTreeFromArray(vector<int> &arr)
{
    if (arr.empty() || arr[0] == -1) return nullptr;

    TreeNode *root = new TreeNode(arr[0]);
    queue<TreeNode *> q;
    q.push(root);

    int i = 1;
    while (!q.empty() && i < arr.size())
    {
        TreeNode *node = q.front();
        q.pop();

        // 左子节点
        if (i < arr.size() && arr[i] != -1)
        {
            node->left = new TreeNode(arr[i]);
            q.push(node->left);
        }
        i++;

        // 右子节点
        if (i < arr.size() && arr[i] != -1)
        {
            node->right = new TreeNode(arr[i]);
            q.push(node->right);
        }
        i++;
    }

    return root;
}

// 辅助函数：层序遍历打印二叉树
void printTree(TreeNode *root)
{
    if (!root)
    {
        cout << "[]" << endl;
        return;
    }

    queue<TreeNode *> q;
    q.push(root);
    vector<string> result;

    while (!q.empty())
    {
        TreeNode *node = q.front();
        q.pop();

        if (node)
        {
            result.push_back(to_string(node->val));
            q.push(node->left);
            q.push(node->right);
        } else
        {
            result.push_back("null");
        }
    }

    // 移除末尾的null
    while (!result.empty() && result.back() == "null")
    {
        result.pop_back();
    }

    cout << "[";
    for (int i = 0; i < result.size(); i++)
    {
        cout << result[i];
        if (i < result.size() - 1) cout << ",";
    }
    cout << "]" << endl;
}

// 辅助函数：中序遍历验证BST性质
void inorderTraversal(TreeNode *root, vector<int> &result)
{
    if (!root) return;
    inorderTraversal(root->left, result);
    result.push_back(root->val);
    inorderTraversal(root->right, result);
}

// 辅助函数：验证是否为有效的BST
bool isValidBST(TreeNode *root)
{
    vector<int> inorder;
    inorderTraversal(root, inorder);
    for (int i = 1; i < inorder.size(); i++)
    {
        if (inorder[i] <= inorder[i-1]) return false;
    }
    return true;
}

// 辅助函数：验证所有节点值都在[low, high]范围内
bool allNodesInRange(TreeNode *root, int low, int high)
{
    if (!root) return true;
    if (root->val < low || root->val > high) return false;
    return allNodesInRange(root->left, low, high) && allNodesInRange(root->right, low, high);
}

// 辅助函数：释放二叉树内存
void deleteTree(TreeNode *root)
{
    if (!root) return;
    deleteTree(root->left);
    deleteTree(root->right);
    delete root;
}

int main()
{
    Solution solution;

    cout << "=== LeetCode 669. 修剪二叉搜索树 测试案例 ===" << endl << endl;

    // 测试案例1：题目示例1
    cout << "测试案例1：" << endl;
    vector<int> tree1 = {1, 0, 2};
    TreeNode *root1 = buildTreeFromArray(tree1);
    int low1 = 1, high1 = 2;

    cout << "输入: root = ";
    printTree(root1);
    cout << "      low = " << low1 << ", high = " << high1 << endl;

    TreeNode *result1 = solution.trimBST(root1, low1, high1);
    cout << "输出: ";
    printTree(result1);
    cout << "验证BST性质: " << (isValidBST(result1) ? "通过" : "失败") << endl;
    cout << "验证范围约束: " << (allNodesInRange(result1, low1, high1) ? "通过" : "失败") << endl << endl;

    // 测试案例2：题目示例2
    cout << "测试案例2：" << endl;
    vector<int> tree2 = {3, 0, 4, -1, 2, -1, -1, -1, -1, 1};
    TreeNode *root2 = buildTreeFromArray(tree2);
    int low2 = 1, high2 = 3;

    cout << "输入: root = ";
    printTree(root2);
    cout << "      low = " << low2 << ", high = " << high2 << endl;

    TreeNode *result2 = solution.trimBST(root2, low2, high2);
    cout << "输出: ";
    printTree(result2);
    cout << "验证BST性质: " << (isValidBST(result2) ? "通过" : "失败") << endl;
    cout << "验证范围约束: " << (allNodesInRange(result2, low2, high2) ? "通过" : "失败") << endl << endl;

    // 测试案例3：空树
    cout << "测试案例3（空树）：" << endl;
    TreeNode *root3 = nullptr;
    int low3 = 1, high3 = 3;

    cout << "输入: root = ";
    printTree(root3);
    cout << "      low = " << low3 << ", high = " << high3 << endl;

    TreeNode *result3 = solution.trimBST(root3, low3, high3);
    cout << "输出: ";
    printTree(result3);
    cout << "验证BST性质: " << (result3 == nullptr || isValidBST(result3) ? "通过" : "失败") << endl << endl;

    // 测试案例4：所有节点都在范围内
    cout << "测试案例4（所有节点都在范围内）：" << endl;
    vector<int> tree4 = {3, 2, 4, 1, -1, -1, 5};
    TreeNode *root4 = buildTreeFromArray(tree4);
    int low4 = 1, high4 = 5;

    cout << "输入: root = ";
    printTree(root4);
    cout << "      low = " << low4 << ", high = " << high4 << endl;

    TreeNode *result4 = solution.trimBST(root4, low4, high4);
    cout << "输出: ";
    printTree(result4);
    cout << "验证BST性质: " << (isValidBST(result4) ? "通过" : "失败") << endl;
    cout << "验证范围约束: " << (allNodesInRange(result4, low4, high4) ? "通过" : "失败") << endl << endl;

    // 测试案例5：所有节点都不在范围内
    cout << "测试案例5（所有节点都不在范围内）：" << endl;
    vector<int> tree5 = {3, 2, 4, 1, -1, -1, 5};
    TreeNode *root5 = buildTreeFromArray(tree5);
    int low5 = 6, high5 = 10;

    cout << "输入: root = ";
    printTree(root5);
    cout << "      low = " << low5 << ", high = " << high5 << endl;

    TreeNode *result5 = solution.trimBST(root5, low5, high5);
    cout << "输出: ";
    printTree(result5);
    cout << "验证BST性质: " << (result5 == nullptr || isValidBST(result5) ? "通过" : "失败") << endl;
    cout << "验证范围约束: " << (allNodesInRange(result5, low5, high5) ? "通过" : "失败") << endl << endl;

    // 测试案例6：只保留根节点
    cout << "测试案例6（只保留根节点）：" << endl;
    vector<int> tree6 = {3, 1, 5, 0, 2, 4, 6};
    TreeNode *root6 = buildTreeFromArray(tree6);
    int low6 = 3, high6 = 3;

    cout << "输入: root = ";
    printTree(root6);
    cout << "      low = " << low6 << ", high = " << high6 << endl;

    TreeNode *result6 = solution.trimBST(root6, low6, high6);
    cout << "输出: ";
    printTree(result6);
    cout << "验证BST性质: " << (isValidBST(result6) ? "通过" : "失败") << endl;
    cout << "验证范围约束: " << (allNodesInRange(result6, low6, high6) ? "通过" : "失败") << endl << endl;

    // 测试案例7：修剪左子树
    cout << "测试案例7（修剪左子树）：" << endl;
    vector<int> tree7 = {5, 3, 7, 1, 4, 6, 8};
    TreeNode *root7 = buildTreeFromArray(tree7);
    int low7 = 4, high7 = 8;

    cout << "输入: root = ";
    printTree(root7);
    cout << "      low = " << low7 << ", high = " << high7 << endl;

    TreeNode *result7 = solution.trimBST(root7, low7, high7);
    cout << "输出: ";
    printTree(result7);
    cout << "验证BST性质: " << (isValidBST(result7) ? "通过" : "失败") << endl;
    cout << "验证范围约束: " << (allNodesInRange(result7, low7, high7) ? "通过" : "失败") << endl << endl;

    // 清理内存
    deleteTree(result1);
    deleteTree(result2);
    deleteTree(result3);
    deleteTree(result4);
    deleteTree(result5);
    deleteTree(result6);
    deleteTree(result7);

    cout << "所有测试案例执行完毕！" << endl;

    return 0;
}